import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '@/views/HomeView.vue'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

NProgress.configure({
  easing: 'ease', // 动画方式    
  speed: 500, // 递增进度条的速度    
  showSpinner: false, // 是否显示加载ico    
  trickleSpeed: 200, // 自动递增间隔    
  minimum: 0.3 // 初始化时的最小百分比
})

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
// 重写了原型上的push方法，统一的处理了错误信息
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/',
    name: 'home',
    meta:{
      title:"暄语-主页发现新鲜事"
    },
    component: HomeView
  },
  {
    path: '/PersonView/:id',
    name: 'PersonView',
    meta:{
      title:"暄语-个人主页"
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/PersonView.vue')
  },
  {
    path: '/PostView/:id',
    name: 'PostView',
    meta:{
      title:"暄语-帖子"
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/PostView.vue')
  },
  {
    path: '/reg',
    name: 'RegView',
    meta:{
      title:"暄语-新用户注册"
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/RegView.vue')
  },
  {
    path: '/login',
    name: 'LoginView',
    meta:{
      title:"暄语-用户登录"
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/LoginView.vue')
  },
  {
    path: '/resetPassword',
    name: 'resetPasswordView',
    meta:{
      title:"暄语-用户登录"
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/resetPasswordView.vue')
  },
  {
    path: '/resetUserPass',
    name: 'resetUserPassView',
    meta:{
      title:"暄语-用户登录"
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/resetUserPassView.vue')
  },
  {
    path: '/Auth',
    name: 'AuthView',
    meta:{
      title:"暄语-用户认证"
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/AuthView.vue')
  },
  {
    path: '/Search',
    name: 'SearchView',
    meta:{
      title:"暄语-搜索"
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/SearchView.vue')
  },
  {
    path: '/Setting',
    name: 'SettingView',
    meta:{
      title:"暄语-设置"
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/SettingView.vue')
  },
  {
    path: '/language',
    name: 'Setting_LanguageView',
    meta:{
      title:"暄语-显示语言"
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/SettingView/Setting_LanguageView.vue')
  },
  {
    path: '/Personal',
    name: 'EditPersonalView',
    meta:{
      title:"暄语-个人资料"
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/EditPersonalView.vue')
  },  
  {
    path: '/YouFansList/:uid/:sid',
    name: 'YouFansListView',
    meta:{
      title:"暄语-粉丝列表"
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/YouFansListView.vue')
  },
  {
    path: '/Notify',
    name: 'NotifyView',
    meta:{
      title:"暄语-粉丝列表"
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/NotifyView.vue')
  },
  {
    path: '/404',
    name: '404View',
    meta:{
      title:"暄语-404"
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/BlankPageView.vue')
  },
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//路由验证登录
router.beforeEach(function (to, from, next) {
  NProgress.start();
  //从存储中拿出token进行判断
  var token = localStorage.getItem("token");
  //排除注册与登录页面
  if (to.path == '/reg'||to.path=='/resetPassword'||to.path==('/resetUserPass')) {
    next()
    return
  }  
  if(token!=null||to.path==='/login'){
    next();    
  }
  else { 
    if (token === null || token === '') {
      next('/login');
    } 
  }
})


router.afterEach(() => {
  // 在即将进入新的页面组件前，关闭掉进度条
  NProgress.done()
})

export default router
